"""
毛玻璃风格密码对话框 - 使用Qt实现高质量毛玻璃效果
"""

from PySide2.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QPushButton,
                               QLabel, QLineEdit, QWidget, QGraphicsDropShadowEffect)
from PySide2.QtCore import Qt, QPropertyAnimation, QEasingCurve, QRect, QTimer
from PySide2.QtGui import (QFont, QPainter, QBrush, QColor, QPen, QLinearGradient,
                          QRadialGradient, QPainterPath, QPixmap, QBitmap)


class GlassmorphismPasswordDialog(QDialog):
    """毛玻璃风格密码对话框"""

    def __init__(self, parent=None):
        super().__init__(parent)
        self.password = ""
        self.setup_dialog()
        self.setup_ui()
        self.setup_animations()

    def setup_dialog(self):
        """设置对话框基本属性"""
        self.setWindowTitle("付费区访问验证")
        self.setFixedSize(450, 320)  # 增加高度，确保按钮完全显示
        self.setModal(True)
        self.setWindowFlags(Qt.Dialog | Qt.FramelessWindowHint)

        # 设置透明背景
        self.setAttribute(Qt.WA_TranslucentBackground)

    def setup_ui(self):
        """设置UI界面"""
        # 主布局 - 使用容器widget来承载内容
        self.main_layout = QVBoxLayout()
        self.main_layout.setContentsMargins(40, 30, 40, 30)
        self.main_layout.setSpacing(25)

        # 应用毛玻璃效果
        self.apply_glassmorphism_effect()

        # 添加顶部间距
        self.main_layout.addSpacing(20)

        # 密码标签 - 居中显示
        password_label = QLabel("密码：")
        password_label.setAlignment(Qt.AlignCenter)
        password_label.setStyleSheet("""
            QLabel {
                color: rgba(255, 255, 255, 0.9);
                font-size: 16px;
                font-weight: 500;
                font-family: 'Microsoft YaHei UI', sans-serif;
                margin-bottom: 8px;
                background: transparent;
                border: none;
            }
        """)
        self.main_layout.addWidget(password_label)

        # 密码输入框
        self.password_input = QLineEdit()
        self.password_input.setEchoMode(QLineEdit.Password)
        self.password_input.setPlaceholderText("请输入密码")
        self.password_input.setMinimumHeight(50)
        self.password_input.setFixedWidth(280)  # 固定宽度，确保不超出对话框边界
        self.password_input.setStyleSheet(self.get_input_style())
        self.main_layout.addWidget(self.password_input, 0, Qt.AlignCenter)  # 居中对齐

        # 添加间距
        self.main_layout.addSpacing(20)

        # 按钮容器
        button_container = QWidget()
        button_container.setStyleSheet("""
            QWidget {
                background: transparent;
                border: none;
            }
        """)
        button_layout = QHBoxLayout(button_container)
        button_layout.setContentsMargins(0, 0, 0, 0)
        button_layout.setSpacing(20)

        # 添加弹性空间
        button_layout.addStretch()

        # 取消按钮
        self.cancel_button = QPushButton("取消")
        self.cancel_button.setMinimumSize(120, 45)
        self.cancel_button.setStyleSheet(self.get_cancel_button_style())
        self.cancel_button.clicked.connect(self.reject)
        button_layout.addWidget(self.cancel_button)

        # 确定按钮
        self.confirm_button = QPushButton("确定")
        self.confirm_button.setMinimumSize(120, 45)
        self.confirm_button.setStyleSheet(self.get_confirm_button_style())
        self.confirm_button.clicked.connect(self.accept)
        button_layout.addWidget(self.confirm_button)

        button_layout.addStretch()
        self.main_layout.addWidget(button_container)

        # 添加底部间距
        self.main_layout.addSpacing(20)

        # 设置焦点和回车键
        self.password_input.setFocus()
        self.password_input.returnPressed.connect(self.accept)

    def apply_glassmorphism_effect(self):
        """应用毛玻璃效果 - 与消息对话框相同的样式"""
        # 创建一个容器widget来承载内容，避免主窗口样式影响
        container = QWidget(self)
        container.setGeometry(0, 0, self.width(), self.height())

        # 设置容器的毛玻璃样式 - 粉色主题
        container.setStyleSheet("""
            QWidget {
                background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                    stop:0 rgba(255, 182, 193, 0.4),
                    stop:0.3 rgba(255, 192, 203, 0.35),
                    stop:0.7 rgba(255, 160, 180, 0.32),
                    stop:1 rgba(255, 175, 190, 0.38));
                border: 2px solid rgba(255, 182, 193, 0.5);
                border-radius: 25px;
            }
        """)

        # 将主布局设置到容器上
        if hasattr(self, 'main_layout'):
            container.setLayout(self.main_layout)

    def get_input_style(self):
        """获取输入框样式 - 进一步降低透明度，增加圆角"""
        return """
            QLineEdit {
                background: rgba(255, 255, 255, 0.3) !important;
                border: 2px solid rgba(255, 255, 255, 0.4) !important;
                border-radius: 18px !important;
                color: rgba(255, 255, 255, 0.95) !important;
                font-size: 16px !important;
                font-family: 'Microsoft YaHei UI', sans-serif !important;
                padding: 15px 20px !important;
                selection-background-color: rgba(0, 212, 255, 0.3) !important;
                selection-color: white !important;
                max-width: 280px !important;
                width: 280px !important;
                min-width: 280px !important;
            }
            QLineEdit:focus {
                background: rgba(255, 255, 255, 0.35);
                border: 2px solid rgba(0, 212, 255, 0.6);
                box-shadow: 0 0 20px rgba(0, 212, 255, 0.3);
            }
            QLineEdit:hover {
                background: rgba(255, 255, 255, 0.32);
                border: 2px solid rgba(255, 255, 255, 0.5);
            }
            QLineEdit::placeholder {
                color: rgba(255, 255, 255, 0.7);
            }
        """

    def get_cancel_button_style(self):
        """获取取消按钮样式 - 进一步降低透明度，增加圆角"""
        return """
            QPushButton {
                background: rgba(255, 255, 255, 0.3);
                color: rgba(255, 255, 255, 0.9);
                border: 2px solid rgba(255, 255, 255, 0.6);
                border-radius: 15px;
                font-size: 14px;
                font-weight: 600;
                font-family: 'Microsoft YaHei UI', sans-serif;
            }
            QPushButton:hover {
                background: rgba(255, 255, 255, 0.35);
                color: rgba(255, 255, 255, 0.95);
                border: 2px solid rgba(255, 255, 255, 0.6);
            }
            QPushButton:pressed {
                background: rgba(255, 255, 255, 0.4);
                border: 2px solid rgba(255, 255, 255, 0.6);
            }
        """

    def get_confirm_button_style(self):
        """获取确定按钮样式 - 增加圆角"""
        return """
            QPushButton {
                background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                    stop:0 rgba(0, 201, 255, 0.8),
                    stop:0.5 rgba(102, 126, 234, 0.8),
                    stop:1 rgba(118, 75, 162, 0.8));
                color: white;
                border: 2px solid rgba(0, 212, 255, 0.6);
                border-radius: 15px;
                font-size: 14px;
                font-weight: 700;
                font-family: 'Microsoft YaHei UI', sans-serif;
            }
            QPushButton:hover {
                background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                    stop:0 rgba(0, 201, 255, 0.9),
                    stop:0.5 rgba(102, 126, 234, 0.9),
                    stop:1 rgba(118, 75, 162, 0.9));
                border: 2px solid rgba(0, 212, 255, 0.6);
            }
            QPushButton:pressed {
                background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                    stop:0 rgba(0, 201, 255, 1.0),
                    stop:0.5 rgba(102, 126, 234, 1.0),
                    stop:1 rgba(118, 75, 162, 1.0));
            }
        """

    def setup_animations(self):
        """设置动画效果"""
        # 淡入动画 - 现在直接对对话框本身进行动画
        self.fade_animation = QPropertyAnimation(self, b"windowOpacity")
        self.fade_animation.setDuration(300)
        self.fade_animation.setEasingCurve(QEasingCurve.OutCubic)

        # 启动动画
        QTimer.singleShot(50, self.start_entrance_animation)

    def start_entrance_animation(self):
        """启动入场动画"""
        # 从透明到不透明的淡入效果
        self.setWindowOpacity(0)
        self.fade_animation.setStartValue(0.0)
        self.fade_animation.setEndValue(1.0)
        self.fade_animation.start()

    def paintEvent(self, event):
        """自定义绘制事件 - 绘制背景"""
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)

        # 绘制背景渐变
        gradient = QLinearGradient(0, 0, self.width(), self.height())
        gradient.setColorAt(0, QColor(255, 107, 107, 80))  # 橙红色
        gradient.setColorAt(0.25, QColor(78, 205, 196, 80))  # 青色
        gradient.setColorAt(0.5, QColor(69, 90, 120, 100))   # 深蓝色
        gradient.setColorAt(0.75, QColor(255, 107, 107, 80)) # 橙红色
        gradient.setColorAt(1, QColor(142, 68, 173, 100))    # 紫色

        painter.fillRect(self.rect(), QBrush(gradient))

        # 绘制装饰圆圈
        self.draw_decorative_circles(painter)

        super().paintEvent(event)

    def draw_decorative_circles(self, painter):
        """绘制装饰性圆圈"""
        painter.setOpacity(0.1)

        # 圆圈1
        gradient1 = QRadialGradient(100, 80, 100)
        gradient1.setColorAt(0, QColor(255, 107, 107))
        gradient1.setColorAt(1, QColor(78, 205, 196))
        painter.setBrush(QBrush(gradient1))
        painter.setPen(Qt.NoPen)
        painter.drawEllipse(50, 30, 200, 200)

        # 圆圈2
        gradient2 = QRadialGradient(400, 200, 120)
        gradient2.setColorAt(0, QColor(69, 90, 120))
        gradient2.setColorAt(1, QColor(150, 200, 255))
        painter.setBrush(QBrush(gradient2))
        painter.drawEllipse(300, 100, 240, 240)

        # 圆圈3
        gradient3 = QRadialGradient(150, 280, 80)
        gradient3.setColorAt(0, QColor(240, 147, 251))
        gradient3.setColorAt(1, QColor(245, 87, 108))
        painter.setBrush(QBrush(gradient3))
        painter.drawEllipse(100, 240, 160, 160)

        painter.setOpacity(1.0)

    def get_password(self):
        """获取输入的密码"""
        return self.password_input.text()

